/**
 * 
 */
package kr.co.insoft.core.service.template.application.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import kr.co.insoft.auth.model.security.User;
import kr.co.insoft.core.model.template.application.ParamEntityModel;
import kr.co.insoft.core.repository.template.application.ParamRepository;
import kr.co.insoft.core.service.template.application.ParamService;
import kr.co.insoft.framework.exceptions.ExceptionCode;
import kr.co.insoft.framework.exceptions.RepositoryException;
import kr.co.insoft.framework.exceptions.ServiceException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author IN-SOFT Inc. Park Jong Taek(jtpark@in-soft.co.kr)
 * 
 */
@Service
public class ParamServiceImpl implements ParamService {
	
	@Autowired
	ParamRepository paramRepository;
	
	@Override
	public List<ParamEntityModel> getList(Map<String, Object> param)
			throws ServiceException {
		try {
			return paramRepository.getList(param);
		} catch (RepositoryException e) {
			throw new ServiceException(ExceptionCode.SERVICE_EXCEPTION, e);
		}
	}
	
	@Override
	@Transactional(rollbackFor = ServiceException.class)
	public void createParam(ParamEntityModel paramEntityModel)
			throws ServiceException {
		try {
			Authentication auths = SecurityContextHolder.getContext()
					.getAuthentication();
			paramEntityModel.setAuth((User) auths.getPrincipal());
			/* Param 등록 */
			paramEntityModel = paramRepository.createParam(paramEntityModel);
		} catch(Exception e) {
			throw new ServiceException(ExceptionCode.SERVICE_EXCEPTION, e);
		}
	}

	@Override
	public ParamEntityModel getParam(String paramid)
			throws ServiceException {
		try {
			Authentication auths = SecurityContextHolder.getContext()
					.getAuthentication();

			ParamEntityModel paramEntityModel = new ParamEntityModel();
			paramEntityModel.setParamid(paramid);
			paramEntityModel.setAuth((User) auths.getPrincipal());
			return paramRepository.getParam(paramEntityModel);

		} catch (RepositoryException e) {
			throw new ServiceException(ExceptionCode.SERVICE_EXCEPTION, e);
		}
	}
	
	@Override
	@Transactional(rollbackFor = ServiceException.class)
	public void doEditParam(ParamEntityModel paramEntityModel)
			throws ServiceException {
		try {
			Authentication auths = SecurityContextHolder.getContext()
					.getAuthentication();
			paramEntityModel.setAuth((User) auths.getPrincipal());

			paramRepository.doEditParam(paramEntityModel);

		} catch (Exception e) {
			throw new ServiceException(ExceptionCode.SERVICE_EXCEPTION, e);
		}
	}

	@Override
	@Transactional(rollbackFor = ServiceException.class)
	public void doRemoveParam(String paramid)
			throws ServiceException {
		try {
			Map<String, Object> param = new HashMap<String, Object>();
			param.put("paramid", paramid);
			paramRepository.doRemoveParam(param);
		} catch (Exception e) {
			throw new ServiceException(ExceptionCode.SERVICE_EXCEPTION, e);
		}
	}

	@Override
	public boolean isExistParamName(ParamEntityModel paramEntityModel) 
			throws ServiceException {
		try {
			Authentication auths = SecurityContextHolder.getContext()
					.getAuthentication();
			paramEntityModel.setAuth((User) auths.getPrincipal());
			return paramRepository.isExistParamName(paramEntityModel) > 0;
		} catch (RepositoryException e) {
			throw new ServiceException(ExceptionCode.SERVICE_EXCEPTION, e);
		}
	}
	
}
